package wli.web.servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Enumeration;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class IndexServlet
 */
@WebServlet("/")
public class IndexServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public IndexServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		PrintWriter out = response.getWriter();
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		PrintWriter out;
		try {
			String temp = null;
			Enumeration enums = request.getHeaderNames();
			while (enums.hasMoreElements()) {
				temp = "" + enums.nextElement();
				System.out.println("Head name=" + temp + "........."
						+ request.getHeader(temp));
			}

			enums = request.getParameterNames();
			while (enums.hasMoreElements()) {
				temp = "" + enums.nextElement();
				System.out.println("Parameter name=" + temp + "........."
						+ request.getHeader(temp));
			}

			out = response.getWriter();
			boolean session_end = false;

			if (request.getParameter("signout") != null
					&& request.getParameter("signout").equals("true")) {
				session.invalidate();
				session_end = true;
			}

			System.out.println("<br>request.getRemoteUser()="
					+ request.getRemoteUser());
			System.out.println("<br>request.getRemoteAddr()="
					+ request.getRemoteAddr());
			System.out.println("<br>request.getAuthType()="
					+ request.getAuthType());
			System.out.println("<br>request.getAsyncContext()="
					+ request.getAsyncContext());
			System.out.println("<br>request.getUserPrincipal()="
					+ request.getUserPrincipal());

			out.println("<html>");
			out.println("<head>");
			out.println("<title></title>");
			out.println("</head>");
			out.println("<body>");
			out.println("<table border=0 width=90% align=center>");
			RequestDispatcher dispatcher = null;
			
			if (session_end) {
				dispatcher = request.getRequestDispatcher("welcome.jsp");
				dispatcher.forward(request, response);
			}
			
			out.println("<tr><td colspan=2 bgcolor=#EEEEEE>");
			dispatcher = request.getRequestDispatcher("top");
			dispatcher.include(request, response);
			out.println("</td></tr>");

			out.println("<tr><td valign=top width=20%  bgcolor=#EEEEEE>");
			dispatcher = request.getRequestDispatcher("vleft");
			dispatcher.include(request, response);
			/*
			 * out.println("<HR>"); dispatcher =
			 * request.getRequestDispatcher("left"); dispatcher.include(request,
			 * response); out.println("</td>");
			 */

			out.println("<td valign=top width=80% bgcolor=#f2f9f8>");
			dispatcher = request.getRequestDispatcher("vmain");
			dispatcher.include(request, response);
			out.println("<HR>");
			// dispatcher = request.getRequestDispatcher("main");
			// dispatcher.include(request, response);

			out.println("</td></tr>");

			
			 //out.println("<tr><td colspan=2  bgcolor=#EFEFEF>"); 
			 //dispatcher =request.getRequestDispatcher("debug");
			 //dispatcher.include(request, response); out.println("</td></tr>");
			 

			out.println("</table>");
			out.println("</body>");
			out.println("</html>");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
